﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
namespace OtherDevice
{
    [Description("盈拓Plasma器")]

    public class Plasma_盈拓:Plasma_Base
    {
        public Plasma_盈拓(string strName) : base(strName)
        {
            controlFrom = new Form_Plasma();
        }

        public override bool OpenPlasma()
        {
            try
            {
                byte[] SendCommand_Reset = new byte[11];
                SendCommand_Reset[0] = 1;
                SendCommand_Reset[1] = 16;
                SendCommand_Reset[2] = 0;
                SendCommand_Reset[3] = 3;
                SendCommand_Reset[4] = 0;
                SendCommand_Reset[5] = 1;
                SendCommand_Reset[6] = 2;
                SendCommand_Reset[7] = 0;
                SendCommand_Reset[8] = 1;
                int n_CRC16_Rest = BaseDll.ParityType.CRC16(SendCommand_Reset, 9);
                SendCommand_Reset[9] = (byte)(n_CRC16_Rest & 0X00FF);
                SendCommand_Reset[10] = (byte)((n_CRC16_Rest & 0XFF00) >> 8);
                byte[] S = null;
                if (!SendAndReciveData(SendCommand_Reset, ref S, 500))
                    return false;
                Thread.Sleep(1000);
                byte[] SendCommand = new byte[11];
                SendCommand[0] = 1;
                SendCommand[1] = 16;
                SendCommand[2] = 0;
                SendCommand[3] = 0;
                SendCommand[4] = 0;
                SendCommand[5] = 1;
                SendCommand[6] = 2;
                SendCommand[7] = 0;
                SendCommand[8] = 1;
                int n_CRC16 = BaseDll.ParityType.CRC16(SendCommand, 9);
                SendCommand[9] = (byte)(n_CRC16 & 0X00FF);
                SendCommand[10] = (byte)((n_CRC16 & 0XFF00) >> 8);
                return SendAndReciveData(SendCommand, ref S, 500);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Plasma控制失败{ex}");
                return false;
            }

        }
        public override bool Function_SetPlasma功率(int nValue)
        {
            try
            {
                byte[] SendCommand = new byte[11];
                SendCommand[0] = 1;
                SendCommand[1] = 16;
                SendCommand[2] = 0;
                SendCommand[3] = 16;
                SendCommand[4] = 0;
                SendCommand[5] = 1;
                SendCommand[6] = 2;
                SendCommand[7] = (byte)((nValue - nValue % 256) / 256);
                SendCommand[8] = (byte)(nValue % 256);
                int n_CRC16 = BaseDll.ParityType.CRC16(SendCommand, 9);
                SendCommand[9] = (byte)(n_CRC16 & 0X00FF);
                SendCommand[10] = (byte)((n_CRC16 & 0XFF00) >> 8);
                byte[] S = null;
                return SendAndReciveData(SendCommand, ref S, 500);


            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Plasma控制失败{ex}");
                return false;
            }
        }
        public override bool Fucntion_GetPlasma功率(ref int n_Value)
        {
            try
            {
                byte[] SendCommand_Reset = new byte[8];
                SendCommand_Reset[0] = 1;
                SendCommand_Reset[1] = 3;
                SendCommand_Reset[2] = 0;
                SendCommand_Reset[3] = 14;
                SendCommand_Reset[4] = 0;
                SendCommand_Reset[5] = 1;
                int n_CRC16_Rest = BaseDll.ParityType.CRC16(SendCommand_Reset, 6);
                SendCommand_Reset[6] = (byte)(n_CRC16_Rest & 0X00FF);
                SendCommand_Reset[7] = (byte)((n_CRC16_Rest & 0XFF00) >> 8);
                byte[] S = null;
                if (SendAndReciveData(SendCommand_Reset, ref S, 500))
                {
                    int n_Result = S[0];
                    n_Result = S[4] + S[3] * 256;
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Plasma控制失败{ex}");
                return false;
            }
        }

        public override bool Function_GetPlasma气压(ref double d_Air)
        {
            d_Air = 0.5;
            return true;
        }

        public override bool Fucntion_PlasmaOK()
        {
            try
            {
                byte[] SendCommand_Reset = new byte[8];
                SendCommand_Reset[0] = 1;
                SendCommand_Reset[1] = 3;
                SendCommand_Reset[2] = 0;
                SendCommand_Reset[3] = 04;
                SendCommand_Reset[4] = 0;
                SendCommand_Reset[5] = 1;
                int n_CRC16_Rest = BaseDll.ParityType.CRC16(SendCommand_Reset, 6);
                SendCommand_Reset[6] = (byte)(n_CRC16_Rest & 0X00FF);
                SendCommand_Reset[7] = (byte)((n_CRC16_Rest & 0XFF00) >> 8);
                byte[] S = null;
                if (SendAndReciveData(SendCommand_Reset, ref S, 500))
                {
                    if (S[3] == 0 && S[4] == 0)
                        return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Plasma控制失败{ex}");
                return false;
            }
        }
        public override bool ClosePlasma()
        {
            try
            {
                byte[] SendCommand = new byte[11];
                SendCommand[0] = 1;
                SendCommand[1] = 16;
                SendCommand[2] = 0;
                SendCommand[3] = 0;
                SendCommand[4] = 0;
                SendCommand[5] = 1;
                SendCommand[6] = 2;
                SendCommand[7] = 0;
                SendCommand[8] = 0;
                int n_CRC16 = BaseDll.ParityType.CRC16(SendCommand, 9);
                SendCommand[9] = (byte)(n_CRC16 & 0X00FF);
                SendCommand[10] = (byte)((n_CRC16 & 0XFF00) >> 8);
                m_SerialPort.DiscardOutBuffer();
                byte[] S = null;
                if (!SendAndReciveData(SendCommand, ref S, 500))
                {
                    return false;
                }

                Thread.Sleep(500);

                byte[] SendCommand_Reset = new byte[11];
                SendCommand_Reset[0] = 1;
                SendCommand_Reset[1] = 16;
                SendCommand_Reset[2] = 0;
                SendCommand_Reset[3] = 3;
                SendCommand_Reset[4] = 0;
                SendCommand_Reset[5] = 1;
                SendCommand_Reset[6] = 2;
                SendCommand_Reset[7] = 0;
                SendCommand_Reset[8] = 0;
                //SendCommand_Reset[9] = 66;
                //SendCommand_Reset[10] = 27;
                int n_CRC16_Rest = BaseDll.ParityType.CRC16(SendCommand_Reset, 9);
                SendCommand_Reset[9] = (byte)(n_CRC16_Rest & 0X00FF);
                SendCommand_Reset[10] = (byte)((n_CRC16_Rest & 0XFF00) >> 8);

                return SendAndReciveData(SendCommand_Reset, ref S, 500);

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Plasma控制失败{ex}");
                return false;
            }
        }
    }
}
